package com.example.javademo.ui.main;

import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.javademo.Frigment;
import com.example.javademo.R;

public class FrigmentFragment extends Fragment {

    private MainViewModel mViewModel;
    private Frigment frigmentActivity;
    private static final String TAG = "FrigmentFragment";

    public static FrigmentFragment newInstance() {
        return new FrigmentFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.d(TAG, "onAttach: Fragment正在附加到Activity");

        if (context instanceof Frigment) {
            frigmentActivity = (Frigment) context;

            // 获取Activity中的toastMsg方法返回的字符串
            String message = frigmentActivity.toastMsg();

            // 弹窗提示
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: Fragment正在创建");
        mViewModel = new ViewModelProvider(this).get(MainViewModel.class);
        // TODO: Use the ViewModel
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: Fragment正在创建视图");
        View v = inflater.inflate(R.layout.fragment_main, container, false);

        // 从Bundle中获取数据
        String value = null;
        Bundle args = getArguments();
        if (args != null) {
            value = args.getString("key_from_f1");
        }

        // 从View中获取组件
        TextView textView = v.findViewById(R.id.message);
        if (textView != null) {
            // 设置组件的文本内容
            if (value != null) {
                textView.setText(value);
            } else {
                // 处理 value 为 null 的情况
                textView.setText("默认文本");
            }
        }

        return v;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: Fragment可见但还不能交互");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: Fragment可见且可以交互");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: Fragment暂停，部分UI被遮挡");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG, "onStop: Fragment停止，完全不可见");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d(TAG, "onDestroyView: Fragment视图被销毁");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: Fragment被销毁");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.d(TAG, "onDetach: Fragment与Activity分离");
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d(TAG, "onSaveInstanceState: Fragment保存状态");
    }

    @Override
    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        Log.d(TAG, "onViewStateRestored: Fragment恢复状态");
    }
}
